//属性管理的api
import request from '../../../utils/request'
//引入类型
import type {
  CategoryResponseData,
  AttrResponseData,
  ResponseData,
  AttrRequestData,
  AttrValueRequestData,
} from './type'

enum API {
  //获取一级分类的接口
  GET_CATEGORY_LIST = '/admin/product/getCategory1',
  //获取二级分类的接口
  GET_CATEGORY2_LIST = '/admin/product/getCategory2',
  //获取三级分类的接口
  GET_CATEGORY3_LIST = '/admin/product/getCategory3',
  //获取平台属性及属性值接口
  GET_ATTR_LIST = '/admin/product/attrInfoList',
  //添加属性值接口
  ADD_ATTRValue = '/admin/product/saveAttrValue',
  //删除属性值接口
  DELETE_ATTRValue = '/admin/product/deleteAttrValue',

  //属性
  //添加属性
  ADD_ATTR = '/admin/product/saveAttr',
  //删除属性
  DELETE_ATTR = '/admin/product/deleteAttr',
  //修改属性名称
  UPDATE_ATTR = '/admin/product/updateAttr',
}

// 获取一级分类的接口
export const reqCategoryList1 = () =>
  request.get<any, CategoryResponseData>(API.GET_CATEGORY_LIST)
// 获取二级分类的接口
export const reqCategoryList2 = (category1Id: number) =>
  request.get<any, CategoryResponseData>(
    API.GET_CATEGORY2_LIST + `/${category1Id}`,
  )
// 获取三级分类的接口
export const reqCategoryList3 = (category2Id: number) =>
  request.get<any, CategoryResponseData>(
    API.GET_CATEGORY3_LIST + `/${category2Id}`,
  )
//获取平台属性及属性值接口
export const reqAttrList = (category3Id: number) =>
  request.get<any, AttrResponseData>(API.GET_ATTR_LIST + `/${category3Id}`)
//添加属性值接口
export const reqAddAttrValue = (data: any) =>
  request.post<AttrValueRequestData, ResponseData>(API.ADD_ATTRValue, data)
//删除属性值接口
export const reqDeleteAttrValue = (attrId: number) =>
  request.delete<any, ResponseData>(API.DELETE_ATTRValue + `/${attrId}`)

//属性
//添加属性
export const reqAddAttr = (data: AttrRequestData) =>
  request.post<AttrRequestData, ResponseData>(API.ADD_ATTR, data)
//删除属性
export const reqDeleteAttr = (attrId: number) =>
  request.delete<any, ResponseData>(API.DELETE_ATTR + `/${attrId}`)
//修改属性
export const reqUpdateAttr = (data: AttrRequestData) =>
  request.put<AttrRequestData, ResponseData>(API.UPDATE_ATTR, data)

//合并添加和修改属性请求
export const reqAddOrUpdateAttr = (data: AttrRequestData) => {
  if (data.id) {
    return reqUpdateAttr(data)
  } else {
    return reqAddAttr(data)
  }
}
